package com.lyz.lambdas.streamDemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName StreamTest2
 * @Description TODO
 * @Author 2214361854
 * @Date 2022/2/23 14:32
 * @Version 1.0
 */
public class StreamTest2 {
    public static void main(String[] args) {
        List<String> lists = new ArrayList<>();
        Collections.addAll(lists,"张强","张小强","小张强","张部","周晓","赵敏");

//        源码
//        lists.stream().filter(new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                return s.startsWith("张");
//            }
//        });

        //map加工方法 s 加工成 "名字："+s
        Stream<String> mapStream = lists.stream().filter(s -> s.startsWith("张")).map(s -> "名字：" + s);
        List<String> collect = mapStream.collect(Collectors.toList());
        System.out.println(collect);

//        流只能使用一次
//        List<String> collect2 = mapStream.collect(Collectors.toList());

        Stream<String> mapStream2 = lists.stream().filter(s -> s.startsWith("张")).map(s -> "名字：" + s);
//        收集流
        String[] strings = mapStream2.toArray(String[]::new);
        System.out.println(Arrays.toString(strings));
    }
}
